using System;
using Sce.PlayStation.Core;

namespace Nyanimals
{
	public class CNyanimalEatState : CNyamimalBaseState
	{
        float curTime;
        float biteRate;
        CBaseFood food;
        
		public CNyanimalEatState(string szName, CNyanimal nyan) : base(szName, nyan)
		{
		}

		public override void Enter ()
		{
            food = Nyaminal.FoodToEat;
            
            // if we are held by someone else, then leave
            if(food.ObjectHolding != null && food.ObjectHolding != Nyaminal)
            {
                owningMachine.ChangeState("idle");
                return;
            }
            
            food.ObjectHolding = Nyaminal;
			Nyaminal.ChangeAnimation("eat");
            curTime = 0.0f;
            biteRate = Nyaminal.BiteRate;
            
            // Console.WriteLine("NyanimalEatState::Enter()");
		}

		public override void Exit ()
		{
            // drop the food
            if(food.ObjectHolding == Nyaminal)
            {
                food.ObjectHolding = null;
            }
            Nyaminal.FoodToEat = null;
                        
            // remove the food if we cannot eat anymore
            bool bite = food.CanBite();
            if(bite == false)
            {
                CObjectManager.Instance.RemoveObject((CBaseObject)food);
            }
            // Console.WriteLine("NyanimalEatState::Exit()");
		}

		public override void Input ()
		{		
		}

		public override void Update (float deltaTime)
		{
            // take a bite
            curTime += deltaTime;
            if(curTime >= biteRate)
            {
                bool biteResult = food.Bite(Nyaminal);
                bool full = Nyaminal.HungerStatus != 0;
                Console.WriteLine("Hunger at: " + Nyaminal.Hunger);
                
                curTime = 0.0f;
                
                // check if we can leave this state
                if(biteResult == false || full)
                {                 
                    // to idle
                    owningMachine.ChangeState("idle");
                }                
            }
            
            
            
		}
	}
}

